package com.ruoyi.pcwl.wechat;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pcwl.domain.Article;
import com.ruoyi.pcwl.domain.dto.UserLoginDTO;
import com.ruoyi.pcwl.domain.vo.UserLoginVO;
import com.ruoyi.pcwl.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeArticleResult;
import me.chanjar.weixin.mp.bean.draft.WxMpDraftList;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("微信接口管理")
@RestController
@RequestMapping("/wechat/test")
@Log
public class WechatController extends BaseController
{

    @Autowired
    WxMpService wxMpService;

    @Autowired
    IArticleService iArticleService;

    @ApiOperation("测试wechat")
    @GetMapping("/list")
    public AjaxResult userList()
    {

        return success("say hello");
    }

    @ApiOperation("获取微信自由发布文章列表")
    @GetMapping("/openArticleList")
    public AjaxResult openArticleList() throws WxErrorException {

        WxMpFreePublishService freePublishService = wxMpService.getFreePublishService();
        WxMpFreePublishList publicationRecords = freePublishService.getPublicationRecords(0, 90,0);

//        iArticleService.save(publicationRecords);

//        List<Article> list = iArticleService.list();


        return success(publicationRecords);
    }

    @ApiOperation("获取微信群发文章列表")
    @GetMapping("/articleList")
    public AjaxResult articleList() throws WxErrorException {

        WxMpDataCubeService dataCubeService = wxMpService.getDataCubeService();
        List<WxDataCubeArticleResult> articleSummary = dataCubeService.getArticleSummary(DateUtils.parseDate("2024-1-9"), DateUtils.parseDate("2024-1-10"));


        return success(articleSummary);
    }

    @ApiOperation("获取微信草稿列表")
    @GetMapping("/draftList")
    public AjaxResult draftList() throws WxErrorException {

        WxMpDraftService draftService = wxMpService.getDraftService();

        WxMpDraftList wxMpDraftList = draftService.listDraft(0, 10);

        return success(wxMpDraftList);
    }

    @ApiOperation("获取微信已发素材列表")
    @GetMapping("/materialList")
    public AjaxResult materialList() throws WxErrorException {

        WxMpMaterialService materialService = wxMpService.getMaterialService();
//        WxMpMaterialFileBatchGetResult wxMpMaterialFileBatchGetResult = materialService.materialFileBatchGet("news", 0, 20);
//        WxMpMaterialNewsBatchGetResult wxMpMaterialNewsBatchGetResult = materialService.materialNewsBatchGet(0, 20);
        WxMpMaterialCountResult wxMpMaterialCountResult = materialService.materialCount();


        return success(wxMpMaterialCountResult);
    }


//    /**
//     * 微信登录
//     * @param userLoginDTO
//     * @return
//     */
//    @PostMapping("/login")
//    @ApiOperation("微信登录")
//    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
////        log.info("微信用户登录：{}",userLoginDTO.getCode());
//        log.info("微信用户登录：{"+userLoginDTO.getCode()+"}");
//
//        //微信登录
//        User user = userService.wxLogin(userLoginDTO);//后绪步骤实现
//
//        //为微信用户生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.USER_ID,user.getId());
//        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
//
//        UserLoginVO userLoginVO = UserLoginVO.builder()
//                .id(user.getId())
//                .openid(user.getOpenid())
//                .token(token)
//                .build();
//        return Result.success(userLoginVO);
//    }

}

